<?php
/**
 * (exporter)
 */
namespace App\Exporter\Shipping;

use App\Models\Inventory\Timely;
use App\Models\Product\ProductClassify;
use App\Models\Product\ProductSku;
use App\Models\Shipping\SSkuTotal;
use App\Models\Shipping\SSkuTotalApply;
use Encore\Admin\Grid;
use Encore\Admin\Grid\Exporters\ExcelExporter;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Concerns\WithMapping;
class TotalExporter extends ExcelExporter implements WithMapping,WithHeadings
{
    protected $shippingDate;

    public function __construct($shippingDate,Grid $grid = NULL)
    {
        parent::__construct($grid);

        $this->shippingDate = $shippingDate;
    }

    protected $fileName = '提货产品汇总.csv';

    public function headings(): array
    {
        $head1 = ['产品名称','SKU','主SKU','品类','季节性','在库','在途', '在库1','在途1',];
        $head2 = [];
        foreach ($this->shippingDate as $label){
            $head2[] = $label.'需求';
        }
        $head3 = ['当期到货','当期结余','往期结余','实际结余','备注','状态'];
        return array_merge($head1,$head2,$head3);
    }

    public function map($model) : array
    {
        $mPSkuId = $model->sku->m_psku_id ?? 0;
        $instant = Timely::getInstant($model->psku_id);
        $transit = Timely::getTransit($model->psku_id);
        if($mPSkuId){
            if($mPSkuId != $model->psku_id){
                $mPSkuCode = ProductSku::getValueById($mPSkuId,'psku_code');
                $mInstant = Timely::getInstant($mPSkuId);
                $mTransit = Timely::getTransit($mPSkuId);
            }else{
                $mPSkuCode = $model->sku->psku_code ?? '';
                $mInstant = $instant;
                $mTransit = $transit;
            }
        }else{
            $mPSkuCode = '';
            $mInstant = 0;
            $mTransit = 0;
        }
        $data1 = [
            $model->sku->psku_cn ?? '',
            $model->sku->psku_code ?? '',
            $mPSkuCode ?: '',
            $model->skuClassify->title ?? '',
            ProductClassify::$_season[$model->skuClassify->season ?? 3],
            $instant,
            $transit,
            $mInstant,
            $mTransit];
        $data2 = [];
        $_shippingDate = array_keys($this->shippingDate);
        $applyNum = SSkuTotalApply::getApplyNum($model->psku_id,array_key_first($this->shippingDate),array_key_last($this->shippingDate));
        foreach ($this->shippingDate as $date => $label){
            $data2[] = array_key_exists($date,$applyNum) ? $applyNum[$date] : 0;
        }
        $storage_num_0 = SSkuTotal::getStorageNum($_shippingDate[0],$_shippingDate[1],$mPSkuId);
        $differ_num_0 = $storage_num_0 - (array_key_exists($_shippingDate[0],$applyNum) ? $applyNum[$_shippingDate[0]] : 0);
        $r_differ = $differ_num_0 - $model->t_differ;
        $data3 = [
            $storage_num_0,
            $differ_num_0,
            $model->t_differ,
            $r_differ,
            $model->remarks,
            SSkuTotal::$_isLock[$model->is_lock],
        ];
        return array_merge($data1,$data2,$data3);
    }
}
